//
//  HYCreatFileAlertView.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/4.
//

#import "HYCreatFileAlertView.h"
#import <HYUIKit/HYAlertShowAnimate.h>
#import <HYUIKit/UIView+Separator.h>

#define kItemMargin         10*KAdapterScale
#define kClomnCount         (kIsIPad?6:4)
#define kFileItemWidth      (kScreenWidth-kItemMargin*2-(kClomnCount-1)*kItemMargin)/kClomnCount


@interface HYCreatFileAlertView ()

@property (nonatomic, strong) NSArray *itemTypes;

@property (nonatomic, copy) HYCreatFileClickBlock clickBlock;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *lineView;

@end

@implementation HYCreatFileAlertView

- (instancetype)initWithItemTypes:(NSArray *)itemTypes
                    clickBlock:(HYCreatFileClickBlock)clickBlock
{
    self = [super init];
    if(self){
        self.itemTypes = itemTypes;
        self.clickBlock = clickBlock;
        self.backgroundColor = AppContentBgThemeColor;
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = APPMainThemeColor;
        lineView.layer.cornerRadius = 2.0;
        lineView.layer.masksToBounds = YES;
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(2);
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(4.0);
        }];
        __block CGFloat kItemLeft = kItemMargin;
        if(itemTypes.count < kClomnCount){
            kItemLeft = (kScreenWidth - kFileItemWidth *itemTypes.count - kItemMargin*(itemTypes.count-1))/2;
        }
        __block UIView *ggView;
        [itemTypes enumerateObjectsUsingBlock:^(NSNumber *type, NSUInteger idx, BOOL * _Nonnull stop) {
            HYFileItemView *itemView = [[HYFileItemView alloc]init];
            UIButton *btn = [[UIButton alloc]init];
            switch (type.integerValue) {
                case HYCreatFileType_Folder:
                    {
                        btn.tag = HYCreatFileType_Folder+100;
                        [itemView setTitle:NSLocalizedString(@"新建文件夹", nil) imageName:@"add_folder_mode"];
                    }
                    break;
                case HYCreatFileType_Video:
                    {
                        btn.tag = HYCreatFileType_Video+100;
                        [itemView setTitle:NSLocalizedString(@"新建视频", nil) imageName:@"add_video_mode"];
                    }
                    break;
                case HYCreatFileType_Audio:
                    {
                        btn.tag = HYCreatFileType_Audio+100;
                        [itemView setTitle:NSLocalizedString(@"新建音频", nil) imageName:@"add_audio_mode"];
                    }
                    break;
                case HYCreatFileType_Import:
                    {
                        btn.tag = HYCreatFileType_Import+100;
                        [itemView setTitle:NSLocalizedString(@"文件导入", nil) imageName:@"add_import_mode"];
                    }
                    break;
                case HYCreatFileType_Photo:
                    {
                        btn.tag = HYCreatFileType_Photo+100;
                        [itemView setTitle:NSLocalizedString(@"相册导入", nil) imageName:@"add_photo_mode"];
                    }
                    break;
                case HYCreatFileType_Rename:
                    {
                        btn.tag = HYCreatFileType_Rename+100;
                        [itemView setTitle:NSLocalizedString(@"重命名", nil) imageName:@"add_modify_mode"];
                    }
                    break;
                case HYCreatFileType_Delete:
                    {
                        btn.tag = HYCreatFileType_Delete+100;
                        [itemView setTitle:NSLocalizedString(@"删除", nil) imageName:@"add_delete_mode"];
                    }
                    break;
                case HYCreatFileType_Move:
                    {
                        btn.tag = HYCreatFileType_Move+100;
                        [itemView setTitle:NSLocalizedString(@"移动", nil) imageName:@"add_move_mode"];
                    }
                    break;
                case HYCreatFileType_Lock:
                    {
                        btn.tag = HYCreatFileType_Lock+100;
                        [itemView setTitle:NSLocalizedString(@"设为私密", nil) imageName:@"add_lock_mode"];
                    }
                    break;
                case HYCreatFileType_unLock:
                    {
                        btn.tag = HYCreatFileType_unLock+100;
                        [itemView setTitle:NSLocalizedString(@"解除私密", nil) imageName:@"add_unlock_mode"];
                    }
                    break;
                case HYCreatFileType_unZip:
                    {
                        btn.tag = HYCreatFileType_unZip +100;
                        [itemView setTitle:NSLocalizedString(@"解压", nil) imageName:@"add_zip_mode"];
                    }
                    break;
                case HYCreatFileType_FaceToFace:
                    {
                        btn.tag = HYCreatFileType_FaceToFace+100;
                        [itemView setTitle:NSLocalizedString(@"面对面", nil) imageName:@"add_face_mode"];
                    }
                    break;
                default:
                    break;
            }
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:itemView];
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                if(ggView){
                    if (idx % kClomnCount) {
                        make.left.equalTo(ggView.mas_right).offset(kItemMargin);
                        make.top.mas_equalTo(ggView.mas_top);
                    }else{
                        make.left.mas_equalTo(kItemLeft);
                        make.top.mas_equalTo(ggView.mas_bottom);
                    }
                }else{
                    make.left.mas_equalTo(kItemLeft);
                    make.top.equalTo(self).offset(kItemMargin);
                }
                make.width.mas_equalTo(kFileItemWidth);
                make.height.mas_equalTo(kFileItemWidth);
            }];
            [self addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(itemView);
            }];
            ggView = itemView;
        }];
    }
    return self;
}

- (void)btnAction:(UIButton *)sender
{
    if(self.clickBlock){
        self.clickBlock(sender.tag-100);
    }
    [HYAlertShowAnimate disappear];
}

- (void)show
{
    NSInteger row = (self.itemTypes.count-1)/kClomnCount+1;
    CGFloat height = 2*kItemMargin+row*kFileItemWidth+HOME_BAR_HEIGHT;
    self.frame = CGRectMake(0, 0, kScreenWidth, height);
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [HYAlertShowAnimate showInView:window alertView:self popupMode:View_Popup_Mode_Down bgAlpha:0.3 outsideDisappear:YES];
}

@end

@interface HYFileItemView ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation HYFileItemView

- (instancetype)init
{
    self = [super init];
    if(self){
        self.titleLabel.font = kIsIPad?FONT_BOLD_SIZE(14):FONT_SIZE(12);
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.mas_equalTo(kIsIPad?10:5);
            make.width.height.mas_equalTo(44);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageView.mas_bottom).offset(kIsIPad?10:5);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(20);
        }];
    }
    return self;
}

- (void)setTitle:(NSString *)title imageName:(NSString *)imageName
{
    self.titleLabel.text = title;
    self.imageView.image = [UIImage imageNamed:imageName];
}

- (UIImageView *)imageView
{
    if(!_imageView){
        _imageView = [[UIImageView alloc]init];
    }
    return _imageView;
}

- (UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = APPTextThemeColor;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

@end
